package util;

import com.github.pagehelper.Page;
import org.smart4j.framework.helper.BeanHelper;

import java.util.List;

/**
 * 功能概要：
 * 
 * @author linbingwen
 * @since  2015年10月22日 
 */


public class BeanUtil {


    /**
     * 封装成PagedResult
     * @param datas
     * @param <T>
     * @return
     */
    public static <T> PagedResult<T> toPagedResult(List<T> datas) {
        PagedResult<T> result = new PagedResult<T>();
        if (datas instanceof Page) {
            Page page = (Page) datas;
            result.setPageNo(page.getPageNum());
            result.setPageSize(page.getPageSize());
            result.setDataList(page.getResult());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
        }
        else {
            result.setPageNo(1);
            result.setPageSize(datas.size());
            result.setDataList(datas);
            result.setTotal(datas.size());
        }

        return result;
    }

    /**
     * 根据targetClass封装成PagedResult
     * @param datas
     * @param <T>
     * @return
     */
    public static <T> PagedResult<T> toPagedResult(List datas, Class<T> targetClass) {
        PagedResult<T> result = new PagedResult<T>();
        if (datas instanceof Page) {
            Page page = (Page) datas;
            result.setPageNo(page.getPageNum());
            result.setPageSize(page.getPageSize());
            result.setDataList(BeanHelper.mapList(page.getResult(),targetClass));
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
        }
        else {
            result.setPageNo(1);
            result.setPageSize(datas.size());
            result.setDataList(datas);
            result.setTotal(datas.size());
        }

        return result;
    }
}
